<?php
/**
 * Created by PhpStorm.
 * User: 胡毅   ktv
 * Date: 2016/1/22
 * Time: 16:23
 */
class UserAction extends Action{
    public function ktv(){
        $model=M("user");
        if(isset($_SESSION['phone'])) {
            $phone = $_SESSION['phone'];
            $info = $model->where("phone=" . $phone)->find();
            $_SESSION['pId'] = $info['pId'];  //把查询到的手机号码用户id存入session
            $pidList = $info['pId'];

            $sql1 = "select id,yhpic from pics where id not in($pidList) and yhpic like 'flsh5-2haodifang%'";  //查询用户没使用图片的id    uId in($uid) or
            $sql2 = "select id,yhpic from pics where id in($pidList) and yhpic like 'flsh5-2haodifang%'";       //查询用户使用过的图片
            $images2 = M("pics")->query($sql2);
            $images = M("pics")->query($sql1);

            $this->assign("info", $info);
            $this->assign("images2", $images2);//查询用户使用过的图片
            $this->assign("images", $images);//查询用户没使用图片
        }
       $this->display('ktv');
    }

    //检测手机号码   用户没有登陆的状态下
    public function check(){
           if(isset($_POST['button'])) {
               $model = M("user");
               $phone = $_POST['phone'];
               $_SESSION['phone'] = $_POST['phone'];
               $info = $model->where("phone=" . $phone)->find();
               if ($info) {
                   $_SESSION['pId'] = $info['pId'];  //把查询到的手机号码用户id存入session
                   $pidList = $info['pId'];
//flsh5-2haodifang_17.png
                   $sql1 = "select id,yhpic from pics where id not in($pidList) and yhpic like 'flsh5-2haodifang%'";  //查询用户没使用图片的id    uId in($uid) or
                   $sql2 = "select id,yhpic from pics where id in($pidList) and yhpic like 'flsh5-2haodifang%'";     //查询用户使用过的图片
                   $images2 = M("pics")->query($sql2);
                   $images = M("pics")->query($sql1);

                   $this->assign("info", $info);
                   $this->assign("images2", $images2);//查询用户使用过的图片
                   $this->assign("images", $images);//查询用户没使用图片
                   $this->display("ktv");
               } else {
                   $this->redirect("User/ktv");
                   exit;
               }
           }else{
             $this->redirect("User/ktv");
          }
        }
    //确定使用优惠券后改变状态入库
    public function hit(){
        $model=M('user');
       $pid=$_GET['pid'];
        $uid=$_GET['uid'];  //将图片id放入用户id
        $pids=$model->where("id=".$uid)->field('pId')->find();
        $pidList=$pids['pId'].','.$pid;
        $data=array(
            'id'=>$uid,
            'pId'=>trim($pidList,','),
        );
       if($model->save($data)) {
           $this->ajaxReturn("使用成功");
       }else{
           $this->ajaxReturn("使用失败");
       }
    }
}